package com.example.stroebackstage.servlet;
import com.example.stroebackstage.entity.Customer;
import com.example.stroebackstage.exception.CustomerException;
import com.example.stroebackstage.service.CustomerService;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

public class CustomerServlet extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        String toDo = req.getServletPath();
//        展示全部
        if (toDo.equals("/toShowAllCustomer")){
            ArrayList<Customer> customerList = new CustomerService().selectAll();
            req.getSession().setAttribute("customerList", customerList);
        }
//        查询用户
        if (toDo.equals("/toFindCustomer")){
            String massage = req.getParameter("search");
            ArrayList<Customer> customerList = new CustomerService().selectByNameOrEmail(massage);
            req.getSession().setAttribute("customerList", customerList);
        }
//        删除用户
        if (toDo.equals("/toDelCustomer")){
            String email = req.getParameter("email");
            boolean check = new CustomerService().del(email);
            if (check){
                ArrayList<Customer> customerList = new CustomerService().selectAll();
                req.getSession().setAttribute("customerList", customerList);
            }
        }
//        修改用户
        if (toDo.equals("/toUpdateCustomer")){
            String name = req.getParameter("name");
            String email = req.getParameter("email");
            String password = req.getParameter("password");
            String address = req.getParameter("address");
            String PAC = req.getParameter("PAC");
            String phone = req.getParameter("phone");
            String fax = req.getParameter("fax");
            Customer customer = new Customer(name, email, password, address, PAC,  phone, fax);
            if (new CustomerService().updateCustomer(customer)){
                ArrayList<Customer> customerList = new CustomerService().selectAll();
                req.getSession().setAttribute("customerList", customerList);
            }
        }
//        添加用户
        if (toDo.equals("/toInsertCustomer")){
            String name = req.getParameter("name");
            String email = req.getParameter("email");
            String password = req.getParameter("password");
            String address = req.getParameter("address");
            String PAC = req.getParameter("PAC");
            String phone = req.getParameter("phone");
            String fax = req.getParameter("fax");
            Customer customer = new Customer(name, email, password, address, PAC,  phone, fax);
            if (new CustomerService().insert(customer)){
                ArrayList<Customer> customerList = new CustomerService().selectAll();
                req.getSession().setAttribute("customerList", customerList);
            }

        }
//        重定向到用户页面
        resp.sendRedirect(req.getContextPath()+"/customer.jsp");
    }
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doGet(req,resp);
    }
}
